package com.example.myapplication.Cat;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;

import com.example.myapplication.R;
import com.example.myapplication.ShuJu.Re_Catcomit;
import com.example.myapplication.ShuJu.Re_News;
import com.example.myapplication.apter.CatcomitAdapter;
import com.example.myapplication.apter.NewsAdapter;
import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttp;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class CatActivity extends AppCompatActivity {

    private RecyclerView re_cat;
    private OkHttpClient okHttpClient;
    private Request  request;
    private Re_Catcomit  re_catcomit;
    private Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cat);
        viitview();
        //获取到停车场的信息
        getcatcomit();
    }
    private void getcatcomit() {
        request = new Request.Builder().url("http://124.93.196.45:10001/prod-api/api/park/lot/list").build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                e.printStackTrace();
                Log.d("getHotDate22","xxx");
                System.out.println("json"+"getHotDate22");

            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {

                String json = response.body().string();
                Log.d("getcatcomit",json);
                System.out.println("json"+json);
                Gson gson = new Gson();
                // Log.v("12223",json);
                re_catcomit = gson.fromJson(json, Re_Catcomit.class);
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        binnewdate();
                    }
                });
            }
        });
    }

    private void binnewdate() {
        CatcomitAdapter newsApter = new CatcomitAdapter(this,re_catcomit.getRows());
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        re_cat.setLayoutManager(linearLayoutManager);
        re_cat.setAdapter(newsApter);
    }

    private void viitview() {
        re_cat = findViewById(R.id.re_cat);
        okHttpClient = new OkHttpClient();
        handler = new Handler();

    }
}